package com.NeuglsWorkStudio.UI.FormElement.Component
{
	import com.NeuglsWorkStudio.UI.FormElement.Config.TConfig;
	import com.NeuglsWorkStudio.UI.FormElement.Config.TLabelConfig;
	
	import flash.events.Event;
	import flash.filters.DropShadowFilter;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	public class TLabel extends TComponent
	{
		protected var _FTextField:TextField;
		private var _FTextFormat:TextFormat;
		private var _FCaption:String="Label";
		
		public function TLabel(AConfig:TConfig)
		{
			super(AConfig);
		}
		
		public function get Caption():String
		{
			return _FCaption;
		}

		public function set Caption(value:String):void
		{
			_FCaption = value;
			_FTextField.text=ProcessCaption(value);
		}

		/**The format of the label*/
		public function get ATextFormat():TextFormat
		{
			return _FTextFormat;
		}

		/**
		 * @private
		 */
		public function set ATextFormat(value:TextFormat):void
		{
			_FTextFormat = value;
			_FTextField.setTextFormat(value);
		}
		
		private function GetLabelConfig():TLabelConfig{
			return _FConfig as TLabelConfig;
		}

		override protected function Init():void{
			super.Init();			
			_FTextField=new TextField();
			if(GetLabelConfig().AutoSize){
				_FTextField.autoSize=TextFieldAutoSize.LEFT;
				_FTextField.width=GetLabelConfig().Width;
			}
			_FTextField.antiAliasType=AntiAliasType.ADVANCED;
			
			if(GetLabelConfig().AutoWordWrap){
				_FTextField.wordWrap=true;
				_FTextField.multiline=true;
			}
			if(GetLabelConfig().ATextFormat!=null){
				_FTextFormat=GetLabelConfig().ATextFormat
			}else{
				_FTextFormat=new TextFormat();
				_FTextFormat.font=GetLabelConfig().Font;
				_FTextFormat.size=GetLabelConfig().Size;
				_FTextFormat.color=GetLabelConfig().Color;
				
			}
			_FTextField.defaultTextFormat=_FTextFormat;
			//_FTextField.setTextFormat(_FTextFormat);
			Caption=GetLabelConfig().Caption;
			this.addChild(_FTextField);
			this.mouseChildren=false;
		
			if(GetLabelConfig().AutoCenter){
				this.addEventListener(Event.ADDED_TO_STAGE,Center,false,0,true);
			}
			Paint();
		}
		
		private function Center(E:Event):void{
			if(this.parent){
				this.x=(this.parent.width-this.width)/2;
				this.y=(this.parent.height-this.height)/2;
			}	
		}
		
		protected function Paint():void{
			this.graphics.clear();
		}
		
		protected function ProcessCaption(val:String):String{
			return val;
		}
	}
}